<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 22:26
 */

namespace App\Api;

use App\Domain\Examples\ArticleList as DomainCURD;

use PhalApi\Api;

/**
 *☛☛文章管理√☚☚
 */
class ArticelList extends Api
{
    public function getRules()
    {
        return array(
            'insert' => array(
                'content' => array('name' => 'content', 'require' => true, 'desc' => '文章内容'),
                'title' => array('name' => 'title', 'require' => true, 'desc' => '标题'),
                'typeid' => array('name' => 'typeid', 'type' => 'int', 'desc' => '文章分类ID'),
            ),
            'update' => array(
                'id' => array('name' => 'id', 'require' => true, 'min' => 1, 'desc' => 'ID'),
                'content' => array('name' => 'content', 'require' => false, 'desc' => '文章内容'),
                'title' => array('name' => 'title', 'require' => false, 'desc' => '标题'),
                'typeid' => array('name' => 'typeid', 'type' => 'int', 'desc' => '文章分类ID'),
            ), 'get' => array(
                'id' => array('name' => 'id', 'require' => true, 'min' => 1, 'desc' => 'ID'),
            ),
            'delete' => array(
                'id' => array('name' => 'id', 'require' => true, 'min' => 1, 'desc' => 'ID'),
            ),
            'getList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'min' => 1, 'default' => 1, 'desc' => '第几页'),
                'perpage' => array('name' => 'perpage', 'type' => 'int', 'min' => 1, 'max' => 20, 'default' => 10, 'desc' => '分页数量'),
            )
        );
    }

    /**
     * 插入数据
     * @desc 向数据库插入一条纪录数据
     * @return int id 新增的ID
     * @return boolean result 添加结果
     * @return string msg 结果描述
     */
    public function insert()
    {
        $rs = array();

        $newData = array(
            'content' => $this->content,
            'title' => $this->title,
            'typeid' => $this->typeid,
        );
        $newData['createtime']=date("Y-m-d h:i:sa");
        $newData['updatetime']=date("Y-m-d h:i:sa");
        $domain = new DomainCURD();
        $id = $domain->insert($newData);
        $result = false;
        $result_content = '添加失败，请稍后重试';
        if ($id > 0) {
            $result = true;
            $result_content = '添加成功';
        }
        $rs['result'] = $result;
        $rs['msg'] = $result_content;
        $rs['id'] = $id;
        return $rs;
    }

    /**
     * 更新数据
     * @desc 根据ID更新数据库中的一条纪录数据
     * @return int code 更新的结果，1表示成功，0表示无更新，false表示失败
     */
    public function update()
    {
        $rs = array();
        $content = $this->content;
        $title = $this->title;
        $typeid = $this->typeid;

        if ($this->content) {
            $newData['content']=$content;
        }
        if ($this->title) {
            $newData['title']=$title;
        }
        if ($this->typeid) {
            $newData['typeid']=$typeid;
        }
        $domain = new DomainCURD();
        $code = $domain->update($this->id, $newData);
        $result = false;
        $result_content = '更新失败，请稍后重试';
        if ($code > 0) {
            $result = true;
            $result_content = '更新成功';
        }
        $rs['result'] = $result;
        $rs['msg'] = $result_content;
        return $rs;
    }

    /**
     * 获取数据
     * @desc 根据ID获取数据库中的一条纪录数据
     * @return int      id          主键ID
     * @return string   title       标题
     * @return string   content     内容
     * @return int      state       状态
     * @return string   post_date   发布日期
     */
    public function get()
    {
        $domain = new DomainCURD();
        $data = $domain->get($this->id);

        return $data;
    }

    /**
     * 删除数据
     * @desc 根据ID删除数据库中的一条纪录数据
     * @return int code 删除的结果，1表示成功，0表示失败
     */
    public function delete()
    {
        $rs = array();
        $domain = new DomainCURD();
        $code = $domain->delete($this->id);
        $result = false;
        $result_content = '删除失败，请稍后重试';
        if ($code > 0) {
            $result = true;
            $result_content = '删除成功';
        }
        $rs['result'] = $result;
        $rs['msg'] = $result_content;
        return $rs;
    }

    /**
     * 获取分页列表数据
     * @desc 根据状态筛选列表数据，支持分页
     * @return array    items   列表数据
     * @return int      total   总数量
     * @return int      page    当前第几页
     * @return int      perpage 每页数量
     */
    public function getList()
    {
        $rs = array();

        $domain = new DomainCURD();
        $list = $domain->getList($this->page, $this->perpage);

        $rs['items'] = $list['items'];
        $rs['total'] = $list['total'];
        $rs['page'] = $this->page;
        $rs['perpage'] = $this->perpage;

        return $rs;
    }
}